#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <string.h>

#define NEXT_X(x_pr, a, b, m)	(((a) * (x_pr) + (b)) % (m))
#define SWAP_INT(a, b)	(((a) ^ (b)) && ((b) ^= (a) ^= (b), (a) ^= (b)))

struct prio_queue {
	int *array;
	int size;
	int size_max;
};

/* {{{ fancy Mergesort (unused) */
/*
 * nope. too slow
 *
int
Mergesort__merge(int *p1, int *p2, long long unsigned *mc, const int depth,
                 const int left, const int mid, const int right)
{
	int *array, *cpyarray;
	register int i = left, j = mid + 1, s = left;

#ifdef D
	fprintf(stderr, "mc=%llu, depth=%d, left=%d, mid=%d, right=%d\n",
			*mc, depth, left, mid, right);
#endif

	//if (*mc & 1)
	if (depth & 1)
		array = p1, cpyarray = p2;
	else
		array = p2, cpyarray = p1;

	while (i <= mid && j <= right) {
		if (array[i] <= array[j]) {
			cpyarray[s++] = array[i++];
		} else {
			cpyarray[s++] = array[j++];
		}
	}

	while (i <= mid)
		cpyarray[s++] = array[i++];
	while (j <= right)
		cpyarray[s++] = array[j++];

	assert(i == mid + 1);
	assert(j == right + 1);
	assert(s == right + 1);

	++*mc;
	return 0;
}

int
Mergesort__main(int *a, int *b, long long unsigned *mc, const int depth,
                const int L, const int R)
{
	int M = (L + R) / 2;

	if (L < R) {
		Mergesort__main(a, b, mc, depth + 1, L, M);
		Mergesort__main(a, b, mc, depth + 1, M + 1, R);
		Mergesort__merge(a, b, mc, depth, L, M, R);
	}

	return 0;
}

int
Mergesort(int *array, const int n)
{
	int *tmp_array;
	static long long unsigned merge_count = 0;
	int depth = 0;

	tmp_array = malloc(n * sizeof *tmp_array);
	(void) memcpy(tmp_array, array, n * sizeof *array);

	Mergesort__main(array, tmp_array, &merge_count, depth, 0, n - 1);

#ifdef D
	fprintf(stderr, "merge_count=%llu\n", merge_count);
#endif

	free(tmp_array);
	return 0;
}
 */
/* }}} */

/* {{{ priority queue */
static void
pq_destroy(struct prio_queue *pq)
{
	if (pq->size_max) free(pq->array);
}

static void
heap__sift_down(struct prio_queue *pq, int start)
{
	int parent = start,
	    child,
	    swap;

	while (parent * 2 + 1 < pq->size) {
		child = parent * 2 + 1;
		swap = parent;

		if (pq->array[parent]->d > pq->array[child]->d) {
			swap = child;
#ifdef DEBUG
			fprintf(stderr, "Heap__sift_down(): parent=%d child=%d, swap=%d ; pq->array[parent]->d=%d pq->array[child]->d=%d\n",
					parent, child, swap, pq->array[parent]->d, pq->array[child]->d);
#endif
		}

		if (child+1 < pq->size &&
		    pq->array[swap]->d > pq->array[child+1]->d)
			swap = child+1;

		if (swap != parent) {
			swap_ptrs(&pq->array[parent], &pq->array[swap]);
			SWAP_INT(pq->array[parent]->heap_idx, pq->array[swap]->heap_idx);

			parent = swap;
		} else {
			return;
		}
	}
}

static void
heap__sift_up(struct prio_queue *pq, int start, int end)
{
	register int child = end, parent = (end - 1) / 2;

	/*
	 * 'start' is a kind of left barrier for this function
	 */
        while (child > start)
		if (pq->array[(child - 1) / 2]->d > pq->array[child]->d) {
			swap_ptrs(&pq->array[parent], &pq->array[child]);
			SWAP_INT(pq->array[parent]->heap_idx, pq->array[child]->heap_idx);
#ifdef DEBUG
			fprintf(stderr, "Heap__sift_up(): parent=%d child=%d ; swapped pq->array[parent] = { d=%d heap_idx=%d } with"
					" pq->array[child] = { d=%d heap_idx=%d }\n",
					parent, child, pq->array[parent]->d, pq->array[parent]->heap_idx,
					pq->array[child]->d, pq->array[child]->heap_idx);
#endif

			/*child = parent;
			  parent = (child - 1) / 2;*/
			child = parent--;
			parent /= 2;
		} else {
			break;
		}
}

static void
heap__heapify(struct prio_queue *pq)
{
	register int start = pq->size / 2 - 1;

	while (start >= 0)
		Heap__sift_down(pq, start--);
}

static struct adjlist *
pq_extract_min(struct prio_queue *pq)
{
	struct adjlist *ret = NULL;

	swap_ptrs(&pq->array[0], &pq->array[pq->size - 1]); /* deleting max */
	SWAP_INT(pq->array[0]->heap_idx, pq->array[pq->size-1]->heap_idx);
	--pq->size;
	ret = pq->array[pq->size];
	heap__sift_down(pq, 0);

	return ret;
}

static void
pq_init(struct prio_queue *pq, const int n)
{
	pq->array = (struct adjlist **) calloc(8, sizeof *pq->array);
	pq->size_max = 8;
}

static void
pq_insert(struct adjlist *x, struct prio_queue *pq)
{
	struct adjlist **tmp = NULL;

	if (pq->size + 1 > pq->size_max) {
		tmp = (struct adjlist **) realloc(pq->array, sizeof (void *) * (pq->size_max *= 2));
		if (tmp == NULL) {
			(void) fprintf(stderr, "failed to realloc pq->array (new size = %d)\n",
					pq->size_max);
			assert(tmp != NULL);
		} else {
			pq->array = tmp;
		}
	}
	x->heap_idx = pq->size;
	pq->array[pq->size++] = x;
	heap__sift_up(pq, 0, pq->size - 1);
	//heap__sift_down(pq, (pq->size - 1) / 2); //XXX
#ifdef DEBUG
	fprintf(stderr, "pq_insert(): inserted x->d=%d at position x->heap_idx=%d\n",
			x->d, x->heap_idx);
#endif
}

static signed char
pq_is_empty(struct prio_queue *pq)
{
	return (pq->size ? 0 : 1);
}

static void
pq_print(struct prio_queue *pq, char *delim)
{
	register int i = 0;

	for (i = 0; i < pq->size; i++)
		printf("i=%5d: { d=%d p=%p s=%d heap_idx=%d }%s", i, pq->array[i]->d,
				pq->array[i]->p, pq->array[i]->s, pq->array[i]->heap_idx, delim);
	putchar('\n');
}
/* }}} */

int
main(int argc, char **argv)
{
	int n, a, b, c, m, k;
	int *array;
	int nk, ak, bk, ck, mk;
	register int i = 0, x = 0;

	scanf("%d%d%d%d%d", &n, &m, &a, &b, &c);
	array = malloc(sizeof *array * n);
	array[0] = x = c;
	for (i = 1; i < n; i++) {
		x = array[i] = NEXT_X(x, a, b, m);
#ifdef D
		fprintf(stderr, "i=%d, x=%d\n", i, x);
#endif
	}
	Mergesort(array, n);

	scanf("%d", &k);
	while (k--) {
		scanf("%d%d%d%d%d", &nk, &mk, &ak, &bk, &ck);
		x = ck;
		for (i = n - nk; i < n; i++) {
			array[i] = x = NEXT_X(x, ak, bk, mk);
#ifdef D
		fprintf(stderr, "i=%d, x=%d, nk=%d\n", i, x, nk);
#endif
		}
		Mergesort(array, n);
	}

	for (x = i = 0; i < n; i++) {
		x += array[i];
#ifdef D
		fprintf(stderr, "i=%d, x=%d, array[i]=%d\n", i, x, array[i]);
#endif
	}
	printf("%d\n", x);

	free(array);
	return 0;
}
